package cn.cdutetc.library.service.impl;

import cn.cdutetc.library.entity.dto.MenuDTO;
import cn.cdutetc.library.service.MenuService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 菜单权限服务实现类
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Override
    public List<MenuDTO> getMenusByRole(String role) {
        List<MenuDTO> menus = new ArrayList<>();
        
        // 所有角色都有的基础菜单
        menus.add(new MenuDTO("dashboard", "仪表板", "dashboard.html", "fas fa-tachometer-alt", 1));
        
        switch (role) {
            case "system_admin":
                // 系统管理员拥有所有权限
                menus.add(new MenuDTO("users", "用户管理", "users.html", "fas fa-users", 2));
                menus.add(new MenuDTO("books", "图书管理", "books.html", "fas fa-book", 3));
                menus.add(new MenuDTO("categories", "分类管理", "categories.html", "fas fa-tags", 4));
                menus.add(new MenuDTO("borrows", "借阅管理", "borrows.html", "fas fa-exchange-alt", 5));
                menus.add(new MenuDTO("payments", "财务管理", "payments.html", "fas fa-credit-card", 6));
                menus.add(new MenuDTO("notifications", "通知管理", "notifications.html", "fas fa-bell", 7));
                menus.add(new MenuDTO("backup", "数据管理", "backup.html", "fas fa-database", 8));
                menus.add(new MenuDTO("system", "系统配置", "system.html", "fas fa-cog", 9));
                break;
                
            case "book_admin":
                // 图书管理员权限
                menus.add(new MenuDTO("books", "图书管理", "books.html", "fas fa-book", 2));
                menus.add(new MenuDTO("categories", "分类管理", "categories.html", "fas fa-tags", 3));
                menus.add(new MenuDTO("borrows", "借阅管理", "borrows.html", "fas fa-exchange-alt", 4));
                menus.add(new MenuDTO("payments", "财务管理", "payments.html", "fas fa-credit-card", 5));
                menus.add(new MenuDTO("notifications", "通知管理", "notifications.html", "fas fa-bell", 6));
                break;
                
            case "normal_user":
                // 普通用户权限（只能查看自己的借阅记录）
                menus.add(new MenuDTO("my-borrows", "我的借阅", "borrows.html", "fas fa-exchange-alt", 2));
                menus.add(new MenuDTO("my-payments", "我的费用", "payments.html", "fas fa-credit-card", 3));
                break;
                
            default:
                // 默认只有仪表板
                break;
        }
        
        return menus;
    }

    @Override
    public List<String> getPermissionsByRole(String role) {
        List<String> permissions = new ArrayList<>();
        
        // 所有角色都有的基础权限
        permissions.add("dashboard:view");
        
        switch (role) {
            case "system_admin":
                // 系统管理员拥有所有权限
                permissions.addAll(Arrays.asList(
                    "user:view", "user:create", "user:update", "user:delete", "user:export", "user:import",
                    "book:view", "book:create", "book:update", "book:delete", "book:export", "book:import",
                    "category:view", "category:create", "category:update", "category:delete",
                    "borrow:view", "borrow:create", "borrow:update", "borrow:delete", "borrow:export",
                    "payment:view", "payment:create", "payment:update", "payment:delete", "payment:export",
                    "notification:view", "notification:create", "notification:update", "notification:delete",
                    "backup:view", "backup:create", "backup:delete", "backup:restore",
                    "system:view", "system:update"
                ));
                break;
                
            case "book_admin":
                // 图书管理员权限
                permissions.addAll(Arrays.asList(
                    "book:view", "book:create", "book:update", "book:delete", "book:export", "book:import",
                    "category:view", "category:create", "category:update", "category:delete",
                    "borrow:view", "borrow:create", "borrow:update", "borrow:export",
                    "payment:view", "payment:create", "payment:update", "payment:export",
                    "notification:view", "notification:create", "notification:update"
                ));
                break;
                
            case "normal_user":
                // 普通用户权限
                permissions.addAll(Arrays.asList(
                    "book:view",
                    "borrow:view:own", "borrow:create:own",
                    "payment:view:own"
                ));
                break;
                
            default:
                // 默认只有基础权限
                break;
        }
        
        return permissions;
    }
}